package cn.jly.spring.boot.cloud.config;

import cn.jly.spring.boot.cloud.utils.SpringBeansUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lanyangji
 * @description 异步请求池配置
 * @date 2020/12/7 下午 1:28
 */
@Configuration
public class AsyncRequestPoolConfig implements WebMvcConfigurer {
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 配置异步请求
     *
     * @param configurer
     */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        // 处理超时
        configurer.setDefaultTimeout(60 * 1000);
        configurer.setTaskExecutor(threadPoolTaskExecutor);
        configurer.registerCallableInterceptors(SpringBeansUtils.getBean("timeoutCallableProcessingInterceptor",
                TimeoutCallableProcessingInterceptor.class));
    }

    @Bean
    public TimeoutCallableProcessingInterceptor timeoutCallableProcessingInterceptor() {
        return new TimeoutCallableProcessingInterceptor();
    }
}
